import { smokeStore } from '/@/stores/smoke';
import { useUserInfo } from '/@/stores/userInfo';
import alarmApi from '/@/api/smoke/alarm';
import { getUrl } from '/@/utils/hm';
import { Session } from '/@/utils/storage';
const stores = useUserInfo();

// 原生
export function initws() {
	let wsUrl = getUrl('wsUrl');
	let uid = stores.userInfos.id;
	let token = Session.get('token');
	const url = `${wsUrl}?uid=pc${uid}&token=${encodeURIComponent(token)}`;
	// const url = `${wsUrl}?uid=pc${uid}`;
	let ws: any = new WebSocket(url);

	try {
		// ws连接成功
		ws.onopen = function () {
			// showInfo("WebSocket Server [" + url + "] 连接成功！");
			showInfo('ws 连接成功！');
			getAlarm();
			smokeStore().setWsConn(ws);
		};

		// ws连接关闭
		ws.onclose = function () {
			if (ws) {
				ws.close();
				ws = null;
			}
			// showInfo("WebSocket Server [" + url + "] 连接关闭 close！");
			showInfo('ws 连接关闭 close！');
			smokeStore().setWsStatus(false);
			// initws();
		};

		// ws连接错误
		ws.onerror = function () {
			if (ws) {
				ws.close();
				ws = null;
			}
			// showInfo("WebSocket Server [" + url + "] 连接关闭，err！");
			// showInfo('ws 连接错误 err');
		};

		// ws数据返回处理
		ws.onmessage = function (result: any) {
			let { type, deviceCode, data: wsData } = JSON.parse(result.data);
			if(!wsData) return

			switch (type) {
				case 'alarmMsg':
					getAlarm();
					break;

				case 'images':
					smokeStore().deviceImgs[deviceCode] = wsData.url;
					break;

				case 'deviceParams': // 设备自定义参数
					// smokeStore().deviceParams.hskj_params = wsData.hskj_params;
					smokeStore().setDeviceParams('hskj_params', wsData.hskj_params);
					break;

				case 'videoParams': // 视频参数
					// smokeStore().deviceParams.videoParams = wsData;
					smokeStore().setDeviceParams('videoParams', wsData);
					break;

				case 'wsParams': // 通讯参数
					// smokeStore().deviceParams.wsurl = wsData;
					smokeStore().setDeviceParams('wsurl', wsData);
					break;

				case 'lampParams': // 灯光参数
					smokeStore().setDeviceParams('lampParams', wsData);
					break;

				case 'sdCardInfo': // SD卡参数
					smokeStore().setDeviceParams('sdCardInfo', wsData.storage_list[0]);
					break;

				case 'recordSearch': // SD卡存储搜索
					smokeStore().setDeviceParams('recordSearch', wsData);
					break;

				case 'alarmCfg': // 报警配置
					smokeStore().setDeviceParams('alarmCfg', wsData.param);
					break;

				case 'upgradeProgress': // 升级进度
					smokeStore().setDeviceParams('upgradeProgress', wsData);
					break;
			}
		};
	} catch (e: any) {
		// alert(e.message);
	}
}

function showInfo(msg: any) {
	console.log(msg);
}

export function getAlarm() {
	let communityIds = useUserInfo().communitys.map((item: any) => item.communityId);
	const params = {
		eventType: 1,
		isHandle: 1,
		pageNum: 1,
		pagesize: 5,
		communityIds,
	};

	alarmApi.page(params).then((res) => {
		smokeStore().setAlarmMsg(res.data.list);
	});
}
